using Protocol.Code;
using Protocol.Constant;
using Protocol.Dto.Fight;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OverPanel : UIBase
{
    private void Awake()
    {
        Bind(UIEvent.SHOW_OVER_PANEL);
    }
    public override void Execute(int eventCode, object message)
    {
        switch (eventCode)
        {
            case UIEvent.SHOW_OVER_PANEL:
                RefreshShow(message as OverDto);
                break;
            default:
                break;
        }
    }

    public override void OnDestroy()
    {
        base.OnDestroy();
        btnBack.onClick.RemoveListener(backClick);
    }

    private Text txtWinIdentity;
    private Text txtWinBeen;
    private Button btnBack;

    void Start()
    {
        txtWinIdentity = transform.Find("txtWinIdentity").GetComponent<Text>();
        txtWinBeen = transform.Find("txtWinBeen").GetComponent<Text>();
        btnBack = transform.Find("btnBack").GetComponent<Button>();

        btnBack.onClick.AddListener(backClick);

        setPanelActive(false);
    }
    /// <summary>
    /// 刷新显示
    /// </summary>
    private void RefreshShow(OverDto dto)
    {
        setPanelActive(true);

        // 显示是谁胜利
        txtWinIdentity.text = Identity.GetString(dto.WinIdentity);
        // 判断自己是否胜利
        if (dto.WinUIdList.Contains(Models.GameModel.Id))
        {
            txtWinIdentity.text += "胜利";
            txtWinBeen.text = "欢乐豆 + ";
        }
        else
        {
            txtWinIdentity.text += "失败";
            txtWinBeen.text = "欢乐豆 - ";
        }
        // 显示豆子数
        txtWinBeen.text += dto.BeenCount;
    }

    /// <summary>
    /// 返回的点击事件
    /// </summary>
    private void backClick()
    {
        // 跳转场景
        LoadSceneMsg msg = new LoadSceneMsg(1, delegate ()
        {
            //向服务器获取自身信息
            SocketMsg socketMsg = new SocketMsg(OpCode.USER, UserCode.GET_INFO_CREQ, null);
            Dispatch(AreaCode.NET, 0, socketMsg);
            //Debug.Log("加载完成!");
        });

        // 将场景复原, 为下次进入房间做准备
        Dispatch(AreaCode.UI, UIEvent.RECOVER_PANEL, null);

        Dispatch(AreaCode.Scene, SceneEvent.LOAD_SCENE, msg);
    }
}
